package frame.laxcen.response;

import frame.laxcen.BaseFrame;
import frame.laxcen.util.FrameReader;
import frame.laxcen.exception.IllegalFrameFormatException;

import java.util.HashMap;
import java.util.Map;

// 返回读写器对错误的反馈信息
public class Response_0000_00 extends BaseFrame implements IResponse {

    public Response_0000_00(BaseFrame baseFrame) throws IllegalFrameFormatException {

        initialize(baseFrame.FrameBytes);
    }

    public Response_0000_00(byte[] bytes) throws IllegalFrameFormatException {

        initialize(bytes);
    }

    @Override
    public Map<String, String> explainToMap() throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        try {

            FrameReader reader = new FrameReader(this);

            // 错误类型

            switch(reader.readU8()) {

                case 0: result.put("错误类型", "错误的类型号"); break;
                case 1: result.put("错误类型", "CRC校验错误"); break;
                case 2: result.put("错误类型", "错误的MID"); break;
                case 3: result.put("错误类型", "协议控制字其他错误"); break;
                case 4: result.put("错误类型", "当前状态无法执行该指令"); break;
                case 5: result.put("错误类型", "指令队列已满"); break;
                case 6: result.put("错误类型", "消息参数不完整"); break;
                case 7: result.put("错误类型", "帧长度超过限制"); break;
                case 8: result.put("错误类型", "其他错误"); break;
            }

            // 读写器状态

            switch(reader.readU8()) {

                case 0: result.put("读写器状态", "空闲状态"); break;
                case 1: result.put("读写器状态", "执行状态"); break;
                case 2: result.put("读写器状态", "故障状态"); break;
            }

            // 收到的协议控制字

            String mid = "0x" + Integer.toHexString(Short.toUnsignedInt(reader.readU16()));

            result.put("收到的协议控制字", mid);

            // 收到的消息内容长度

            String len = String.valueOf(Short.toUnsignedInt(reader.readU16()));

            result.put("收到的消息内容长度", len);

        } catch (Exception e) {

            e.printStackTrace();

            throw new IllegalFrameFormatException(e.toString());
        }

        return result;
    }
}
